<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="12">
          <a-col :md="7" :sm="8">
            <a-form-item label="标题" :labelCol="{ span: 6 }" :wrapperCol="{ span: 14, offset: 1 }">
              <a-input placeholder="请输入标题" v-model="queryParam.title"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="7" :sm="8">
            <a-form-item label="状态" :labelCol="{ span: 6 }" :wrapperCol="{ span: 14, offset: 1 }">
              <a-select v-model="queryParam.status" placeholder="请选择" default-value="null" :getPopupContainer="triggerNode => triggerNode.parentNode">
                <a-select-option value="">全部</a-select-option>
                <a-select-option value="1">禁用</a-select-option>
                <a-select-option value="0">启用</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="7" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- <a-tabs :activeKey="active" @change="tabsChange">
      <a-tab-pane key="1" :tab="`首焦图`"></a-tab-pane>
      <a-tab-pane key="2" :tab="`点单入口图`" forceRender></a-tab-pane>
    </a-tabs> -->

    <div class="table-operator" style="border-top: 5px">
      <!-- <a-button type="primary" @click="handleAdd">{{active == '1'? '新增首焦图' : '新增单点入口图'}}</a-button> -->
      <a-button type="primary" @click="handleAdd">{{'新增首焦图'}}</a-button>
    </div>

    <a-table
      ref="table"
      rowKey="id"
      size="middle"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      @change="handleTableChange">
      <span slot="img" slot-scope="text">
        <img :src="text" alt="" class="thumbnail" />
      </span>
      <span slot="status" slot-scope="text, record">
        <a-switch :checked="record.status == 0" @click="handleSwitch(record,$event ? ischoose : noChoose)"></a-switch>
      </span>
      <span slot="action" slot-scope="text, record">
        <a @click="handleEdit(record)">
          <a-icon type="edit"/>
          编辑
        </a>
        <a-popconfirm title="确定删除吗?" @confirm="() =>handleDelete(record.id)">
          <a>删除</a>
        </a-popconfirm>
      </span>
    </a-table>

    <Banner-Modal ref="modalForm" @ok="modalFormOk" :typeNum="queryParam.type"></Banner-Modal>  
  </a-card>
</template>

<script>
import { filterObj } from '@/utils/util';
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import BannerModal from './modules/BannerModal'
import { saveOrUpdateAdvert } from '@/api/marketingCenter/operations'

export default {
  name:'banner',
  mixins: [JeecgListMixin],
  components: { BannerModal },
  data() {
    return {
      ischoose: 0,
      noChoose: 1,
      active:'1',
      queryParam: {
        type: '1'
      },
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 120,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '图片',
          dataIndex: 'url',
          align: 'center',
          width: 100,
          scopedSlots: { customRender: 'img' }
        },
        {
          title: '标题',
          align: 'center',
          dataIndex: 'title'
        },
        {
          title: '排序',
          align: 'center',
          dataIndex: 'sort'
        },
        {
          title: '状态',
          dataIndex: 'status',
          align: 'center',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: "/backend/coffee/pageAdvertVo",
        delete: "/backend/coffee/delAdvertById",
      },
    }
  },
  methods: {
    searchReset() {
      this.queryParam = {
        type: this.active
      }
      this.loadData(1)
    },
    getQueryParams() {
      var param = Object.assign({}, this.queryParam);
      param.current = this.ipagination.current;
      param.pageSize = this.ipagination.pageSize;
      return filterObj(param);
    },
    tabsChange(key) {
      console.log(key);
      this.queryParam.type = key
      this.active = key
      this.loadData(1)
    },
    handleSwitch(record,cover) {
      saveOrUpdateAdvert({'id':record.id, 'type': record.type, 'status':cover}).then(res => {
        if (res.code == 200) {
          this.$message.success(res.msg)
          this.loadData(1)
        } if (res.code == 500) {
          this.$message.warning(res.msg)
        }
      })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
.thumbnail {
  width: 100px;
  height: 100px;
}
</style>